package com.qingyun.edusysteam.controller;

import com.qingyun.edusysteam.dto.MaterialDTO;
import com.qingyun.edusysteam.dto.Result;
import com.qingyun.edusysteam.service.IMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 资料中心 前端控制器
 * </p>
 *
 * @author your_name
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/materials")  // 改为复数形式，与前端一致
public class MaterialController {

    @Autowired
    private IMaterialService materialService;

    @PostMapping("/upload")
    public Result uploadMaterial(
            @RequestParam("title") String title,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "grade", required = false) String grade,
            @RequestParam(value = "subject", required = false) String subject,
            @RequestHeader("Authorization") String token
    ) {
        // 创建DTO对象
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setTitle(title);
        materialDTO.setDescription(description);
        materialDTO.setFile(file);
        materialDTO.setCategory(category);
        materialDTO.setGrade(grade);
        materialDTO.setSubject(subject);

        return materialService.uploadMaterial(materialDTO, token);
    }

    @GetMapping("/list")
    public Result getMaterialList(
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "grade", required = false) String grade,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "subject", required = false) String subject,
            @RequestHeader("Authorization") String token
    ) {
        return materialService.getMaterialList(category, grade, keyword, token, subject);
    }

    @GetMapping("/downloadable")
    public Result getDownloadableMaterials(
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "grade", required = false) String grade,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "subject", required = false) String subject,
            @RequestHeader("Authorization") String token
    ) {
        return materialService.getDownloadableMaterials(category, grade, keyword, token, subject);
    }

    @GetMapping("/my-materials")
    public Result getMyMaterials(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "grade", required = false) String grade,
            @RequestParam(value = "subject", required = false) String subject,
            @RequestHeader("Authorization") String token
    ) {
        return materialService.getMaterialList(category, grade, title, token, subject);
    }

    @GetMapping("/download/{id}")
    public Result downloadMaterial(
            @PathVariable Long id,
            @RequestHeader("Authorization") String token,
            HttpServletResponse response
    ) {
        return materialService.downloadMaterial(id, token, response);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteMaterial(
            @PathVariable Long id,
            @RequestHeader("Authorization") String token
    ) {
        return materialService.deleteMaterial(id, token);
    }
}
